package com.io.webflow.service.impl;

import com.io.config.WrapInt;
import com.io.model.*;
import com.io.protocol.CommandConstants;
import com.io.util.EncryptUtils;
import com.io.util.CommonUtil;
import com.io.webflow.mapper.RegisterMapper;
import com.io.webflow.service.RegisterService;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by yangyx on 2016/7/28.
 */
@Service
@Transactional
public class RegisterServiceImpl implements RegisterService {

    static final Logger logger= LoggerFactory.getLogger(RegisterServiceImpl.class);

    @Autowired
    private RegisterMapper registerMapper;

    public WrapInt<RegisterReq> handlerReqister(RegisterReq registerReq, ChannelHandlerContext ctx) {

        RegisterReq obj=registerMapper.findRegister(registerReq.vin);
        if(null==obj){
            //不存在该用户可以注册
            int i=registerMapper.addRegister(registerReq);
            System.out.println("新增成功。。。。。。 。。。。。。。。。。");
        }else{
            //用户已存在

        }
        return null;
    }

    public WrapInt<RegisterReq> terminalRegister(RegisterReq obj) {
        byte result = CommandConstants.RespResCode_Success;
        RegisterReq registerReq=new RegisterReq();
        try{//查询数据库中是否含有该用户，没有的话退出
            registerReq = registerMapper.getUserByObj(obj);
            if (null == registerReq){
                result=CommandConstants.RespResCode_ServerError;
            }
        }catch (Exception e){
            logger.error("获取终端信息出错,异常信息为  ",e.getMessage() );
            result = CommandConstants.RespResCode_ServerError;
        }
        return new WrapInt<RegisterReq>(registerReq, result);
    }



    public void setNewPassword(String newPassword, TerminalPrincipal principal) {
        registerMapper.updateNewPassword(newPassword, principal.id);
    }
}
